/*
 * Ref.h
 *
 *  Created on: 2011-5-28
 *      Author: yangentao
 */

#ifndef REF_H_
#define REF_H_

#include "pre.h"

template<typename T>
class Ref {
public:
	Ref() :
			val(nullptr) {
	}

	virtual ~Ref() {
		val = nullptr;
	}
	Ref(T& v) :
			val(&v) {
	}
	Ref(const Ref<T>& other):val(other.val){

	}
	Ref<T> operator=(const Ref<T>& other) {
		val = other.val;
		return *this;
	}
public:
	operator T&() const {
		return *val;
	}
	Ref<T>& operator =(T& v) {
		val = &v;
		return *this;
	}
	T* getptr()const {
		return val;
	}
	T& get()const {
		return *val;
	}
private:
	T* val;
};


template<typename T>
class CRef {
public:
	CRef() :
			val(nullptr) {
	}

	virtual ~CRef() {
		val = nullptr;
	}
	CRef(const T& v) :
			val(&v) {
	}
	CRef(const CRef<T>& other):val(other.val){

	}
	CRef<T> operator=(const CRef<T>& other) {
		val = other.val;
		return *this;
	}
public:
	operator const T&() const {
		return *val;
	}
	Ref<T>& operator =(const T& v) {
		val = &v;
		return *this;
	}
	const T* getptr()const {
		return val;
	}
	const T& get()const {
		return *val;
	}
private:
	const T* val;
};

#endif /* REF_H_ */
